<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计页面</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../../styles/common.css"/>
    <link rel="stylesheet" href="../../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="DEVICE-app">
    <div class="container">


        <div class="tableBar">
            <el-button type="primary" size="medium" @click="goBack()">返回</el-button>
        </div>

        <div>
            <span id="echarts01"  style="width: 400px;height:400px; float: left"> </span>
            <span id="echarts02"  style="width: 400px;height:400px;">  </span>
            <span id="echarts03"  style="width: 400px;height:400px;">  </span>

        </div>

        <div>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                :current-page.sync="page"
                @current-change="handleCurrentChange"
        ></el-pagination>
        </div>
        <el-table  :data="tableData"  stripe  class="tableBox" >


            <el-table-column  prop="deviceName" label="设备名称"  /></el-table-column>
            <el-table-column  prop="facilitiesName" label="所属大棚"  /></el-table-column>
         <!--   <el-table-column  prop="facilitiesId" label="所属农业设施id"  /></el-table-column>
            <el-table-column  prop="typeId" label="设备类型id"  /></el-table-column>-->
            <el-table-column  prop="typeName" label="设备类型名称"  /></el-table-column>



            <el-table-column  prop="resultCode" label="返回结果code"  /></el-table-column>
            <el-table-column  prop="resultMessage" label="返回结果说明"  /></el-table-column>
      <!--      <el-table-column  prop="resultData" label="返回结果"  /></el-table-column>-->
            <el-table-column  prop="resultDataValue" label="返回结果核心数据"  /></el-table-column>

            <el-table-column  prop="createTime" label="创建时间"  /></el-table-column>



        </el-table>

    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="设备名称：">
                <el-input v-model="classData.deviceName" placeholder="请输入设备名称" maxlength="14"/>
            </el-form-item>



            <el-form-item label="所属大棚：">
                <el-select v-model="classData.facilitiesId" placeholder="请选择所属大棚"
                           @click.native="selectFacilitiesFun()" >

                    <el-option
                            v-for="item in facilitiesOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                            @click.native="getFacilitiesOptionsValue(item.label)"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="设备类型：">
                <el-select v-model="classData.typeId" placeholder="请选择设备类型"
                           @click.native="selectDeviceTypeFun()" >

                    <el-option
                            v-for="item in deviceTypeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                            @click.native="getDeviceTypeOptionsValue(item.label)"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="设备使用状态：">
                <template>
                    <el-radio v-model="classData.state" label="1">可用</el-radio>
                    <el-radio v-model="classData.state" label="0">不可用</el-radio>
                </template>
            </el-form-item>

            <el-form-item label="设备工作状态：">


                <template>
                    <el-radio v-model="classData.flag" label="1" >可用</el-radio>
                    <el-radio v-model="classData.flag" label="0">不可用</el-radio>
                </template>
            </el-form-item>

            <el-form-item label="硬件描述：">
                <el-input v-model="classData.remark" placeholder="请输入硬件描述" />
            </el-form-item>



        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="classData.dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
        <el-button
                v-if="action != 'edit'"
                type="primary"
                size="medium"
                class="continue"
                @click="submitForm('go')"
        > 保存并继续添加 </el-button>
      </span>
    </el-dialog>
</div>
<script src="../../../plugins/vue/vue.js"></script>
<script src="../../../plugins/element-ui/index.js"></script>
<script src="../../../plugins/axios/axios.min.js"></script>
<script src="../../../js/request.js"></script>
<script src="../../../api/device.js"></script>
<script src="../../../api/deviceDataLogs.js"></script>
<script src="../../../api/facilities.js"></script>
<script src="../../../js/echarts.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#DEVICE-app',
        data() {
            return {
                action: '',
                tableData: [],
                type: '',
                classData: {
                    'title': '添加设备',
                    'dialogVisible': false,
                    'id': '',
                    'deviceName': '',
                    'facilitiesId':'',
                    'facilitiesName':'',
                    'typeId': '',
                    'typeName': '',
                    'state': '1',
                    'flag': '1',
                    'remark': ''

                },
                deviceId:'',
                facilitiesIdSelect:'',
                deviceTypeOptions:[],
                facilitiesOptions:[],
                companyNameList: [],
                companyNameList2: [],
                page: 1,
                pageSize: 5,
                counts:0,
                input:''
            }
        },
        methods: {
            async init() {
                var url = location.search;

                if (url.indexOf("?") != -1) {
                    var str = url.substr(1); //substr()方法返回从参数值开始到结束的字符串；
                    /*alert(str.split("=")[1])*/
                   if("deviceId"==str.split("=")[0]){
                       this.deviceId=str.split("=")[1];

                   }else{
                       this.deviceId='';
                   }


                }

                const params = {
                    page: this.page,//当前页码
                    pageSize: this.pageSize,//每页大小
                    facilitiesId: this.facilitiesIdSelect ?this.facilitiesIdSelect :undefined ,//设施id->大棚id
                    deviceId: this.deviceId ?this.deviceId :undefined ,
                    resultCode: 200   //返回结果码
                };
                await getDeviceDateLogsPage(params).then(res => {
                    console.log(res.data.records)
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records;
                        this.counts = res.data.total;


                        var temperature=[];
                        var humidity=[];
                        var HV=[];
                        var XData=[];
                        var deviceType=1;
                        for(var i=0;i<res.data.records.length;i++){

                            if(res.data.records[i].resultCode==200) {
                                if(res.data.records[i].typeId==1) {
                                    var dateValue = res.data.records[i].resultDataValue
                                    var t = dateValue.split(',')[0].split('=')[1]
                                    var h = dateValue.split(',')[1].split('=')[1]

                                    temperature.push(t)
                                    humidity.push(h)
                                    XData.push(res.data.records[i].createTime)
                                    deviceType=1
                                }

                                if(res.data.records[i].typeId==5) {
                                    var dateValue = res.data.records[i].resultDataValue

                                    HV.push(dateValue)

                                    XData.push(res.data.records[i].createTime)
                                    deviceType=5
                                }
                            }
                        }

                        if(deviceType==1){
                            this.e1(XData,temperature)
                            this.e2(XData,humidity)
                        }else if(deviceType==5) {

                            this.e3(XData, HV)
                        }




                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            clearQuery(){
                this.page = 1;
                this.facilitiesIdSelect='';
                this.input='',
                    this.init();
            },
            // 添加
            addClass(st) {
                this.classData.title = '新增设备';
                this.action = 'add';
                this.classData.dialogVisible = true

            },

            selectDeviceTypeFun(){

                selectDeviceTypeOptions().then(res => {
                    console.log(res.data)
                    const resData=res.data
                    if (String(res.code) === '1') {
                        this.deviceTypeOptions=[]
                        for(var i=0;i <resData.length;i++){
                            this.deviceTypeOptions.push({"value":resData[i].value,"label":resData[i].label})
                        }
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            getDeviceTypeOptionsValue(e){
                this.classData.typeName=e
            },


            selectFacilitiesFun(){
                selectFacilitiesOptions().then(res => {
                    console.log(res.data)
                    const resData=res.data
                    if (String(res.code) === '1') {
                        this.facilitiesOptions=[]
                        for(var i=0;i <resData.length;i++){
                            this.facilitiesOptions.push({"value":resData[i].value,"label":resData[i].label})
                        }
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            getFacilitiesOptionsValue(e){
                this.classData.facilitiesName=e;
            },


            editHandle(dat) {
                console.log(dat)
                //赋值
                this.classData.title = '修改设备';
                this.action = 'edit';
                this.classData.id = dat.id;
                this.classData.deviceName = dat.deviceName;
                this.classData.facilitiesId = dat.facilitiesId;
                this.classData.typeId = dat.typeId;
                this.classData.typeName = dat.typeName;
                this.classData.state = dat.state;
                this.classData.flag = dat.flag;
                this.classData.remark = dat.remark;

                this.classData.dialogVisible = true;//打开修改框
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteDevice(id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                const classData = this.classData;

                if (this.action === 'add') {

                            addDevice(classData).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('添加成功！')
                                    if (!st) {
                                        this.classData.dialogVisible = false
                                    } else {
                                        this.classData = {};
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })

                } else if (this.action === 'edit') {
                    editDevice(classData).then(res => {
                        if (res.code === 1) {
                                this.$message.success('修改成功！');
                                this.classData.dialogVisible = false;
                                this.handleQuery()
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })

                } else {
                    this.$message.error('请输入名称')
                }
            },
            // 全部操作
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.id)
                })
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },

            goBack(){
                window.history.go(-1)
            },
            e1(X,T) {


                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('echarts01'));

                // 指定图表的配置项和数据
                option = {
                    title: {
                        text: '温度'
                    },
                    xAxis: {
                        type: 'category',
                        data: X
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: T,
                            type: 'line',
                            smooth: true
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            },
            e2(X,H) {

                // 基于准备好的dom，初始化echarts实例
                var myChart2 = echarts.init(document.getElementById('echarts02'));

                // 指定图表的配置项和数据
                option2 = {
                    title: {
                        text: '湿度'
                    },
                    xAxis: {
                        type: 'category',
                        data: X
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: H,
                            type: 'line',
                            smooth: true
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart2.setOption(option2);
            },
            e3(X,Hv) {

                // 基于准备好的dom，初始化echarts实例
                var myChart3 = echarts.init(document.getElementById('echarts03'));

                // 指定图表的配置项和数据
                option3 = {
                    title: {
                        text: '光照'
                    },
                    xAxis: {
                        type: 'category',
                        data: X
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: Hv,
                            type: 'line',
                            smooth: true
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart3.setOption(option3);
            }

        },



        created() {
            this.init()
        }


    })
</script>
</body>
</html>